<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>员工信息录入</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layuimini-2/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuimini-2/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名"
                   value="" class="layui-input">
            <tip>填写自己真实姓名。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="1" title="男" checked="">
            <input type="radio" name="sex" value="2" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">手机号</label>
        <div class="layui-input-block">
            <input type="number" name="phone" lay-verify="required" lay-reqtext="手机不能为空"
                   placeholder="请输入手机号" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">身份证号码</label>
        <div class="layui-input-block">
            <input type="email" name="idNum" placeholder="请输入身份证号码" lay-verify="required"
                   lay-reqtext="身份证号码不能为空" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">入职日期</label>
        <div class="layui-input-inline">
            <input type="date" name="hiredate" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">岗位</label>
        <div class="layui-input-inline">
            <select name="stationId" id="station" lay-verify="required" lay-reqtext="岗位不能为空">
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">部门</label>
        <div class="layui-input-inline">
            <select name="deptId" id="dept">
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">工资</label>
        <div class="layui-input-block">
            <input type="text" name="salary" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-pane">
        <label class="layui-form-label">
            <span class="x-red"></span>照片
        </label>
        <div class="layui-input-inline">
            <button type="button" class="layui-btn" id="upload">
                <i class="layui-icon">&#xe67c;</i>上传照片
            </button>
            <input id="img" type="hidden" name="img" value="">
            <img id="image" src="" alt="图片" width="100px">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="/layuimini-2/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'upload'], function () {
        var form = layui.form,
            upload = layui.upload,
            layer = layui.layer,
            $ = layui.$;
        // 文件上传
        upload.render({
            elem: '#upload' //绑定元素
            , url: '/upload.do' //上传接口
            , done: function (res) {
                console.log(res.data)
                $("#img").val(res.data.image);
                $("#image").attr("src", res.data.image);
                // console.log("res=>", res);
                if (res.code == 0) {
                    layer.msg("上传成功!")
                }
                form.render();
            }
            , error: function () {
                layer.msg("上传失败!")
            }
        });


        // 下拉框
        $(document).ready(function () {
            // 部门
            $.ajax({
                url: "/dept/test.do",
                type: "get",
                success: function (obj) {
                    var test = document.getElementById("dept");
                    var htm = "";
                    if (obj.data != null) {
                        for (let i = 0; i < obj.data.length; i++) {
                            htm += '<option value="' + obj.data[i].id + '">' + obj.data[i].name + '</option>'
                        }
                    }
                    var op = '<option value="">--请选择--</option>';
                    op += htm;
                    test.innerHTML = op;
                    form.render()
                },
                error: function () {
                    layer.msg("失败!!!")
                }
            })
            // 岗位
            $.ajax({
                url: "/station/test.do",
                type: "get",
                success: function (obj) {
                    var test = document.getElementById("station");
                    var htm = "";
                    if (obj.data != null) {
                        for (let i = 0; i < obj.data.length; i++) {
                            htm += '<option value="' + obj.data[i].id + '">' + obj.data[i].name + '</option>'
                        }
                    }
                    var op = '<option value="">--请选择--</option>';
                    op += htm;
                    test.innerHTML = op;
                    form.render()
                },
                error: function () {
                    layer.msg("失败!!!")
                }
            })
        })

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url: "/per/add.do",
                type: "post",
                data: data.field,
                success: function (ret) {
                    if (ret.code == 0) {
                        var index = layer.alert("添加成功", function () {
                            // 关闭弹出层
                            layer.close(index);
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        });
                    } else {
                        layer.msg("添加失败!!!");
                    }
                },
                error: function () {
                    layer.msg("服务器繁忙!!!");
                }
            })
            return false;
        });

    });
</script>
</body>
</html>